package com.guli.gulimall.product.service.impl;

import com.alibaba.nacos.client.utils.StringUtils;
import com.guli.gulimall.product.dao.BrandDao;
import com.guli.gulimall.product.dao.CategoryDao;
import com.guli.gulimall.product.entity.BrandEntity;
import com.guli.gulimall.product.entity.CategoryEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.gulimall.product.dao.CategoryBrandRelationDao;
import com.guli.gulimall.product.entity.CategoryBrandRelationEntity;
import com.guli.gulimall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    BrandDao brandDao;

    @Autowired
    CategoryDao categoryDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();

        String brandId = (String) params.get("brandId");

        if(StringUtils.isNotEmpty(brandId)){
            wrapper.eq("brand_id",brandId);
        }


        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryBrandRelationEntity> brandsList(Long catlogId) {

        QueryWrapper<CategoryBrandRelationEntity> w = new QueryWrapper<>();

        w.eq("catelog_id",catlogId);



        return baseMapper.selectList(w);
    }

    @Override
    public boolean save(CategoryBrandRelationEntity entity) {

        Long catelogId = entity.getCatelogId();
        Long brandId = entity.getBrandId();

        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        entity.setCatelogName(categoryEntity.getName());

        BrandEntity brandEntity = brandDao.selectById(brandId);
        entity.setBrandName(brandEntity.getName());

        return super.save(entity);
    }
}